package com.cqxyd.smartsite.controller.vs1;

import com.cqxyd.smartsite.pojo.Heartbeat;
import com.cqxyd.smartsite.pojo.LifterRecord;
import com.cqxyd.smartsite.pojo.VideoRecord;
import com.cqxyd.smartsite.service.DeviceInfoService;
import com.cqxyd.smartsite.service.ProgramInfoService;
import com.cqxyd.smartsite.service.UploadRecordService;
import com.cqxyd.smartsite.util.JSONResult;
import com.cqxyd.smartsite.util.PostHelper;
import com.cqxyd.smartsite.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@ApiVersion(1)
@RequestMapping(value = "{version}/api/video")
@Api(value = "视频监控信息接口", tags = "视频监控信息接口")
public class VideoRecordController {
    private final Logger logger = LoggerFactory.getLogger(VideoRecordController.class);
    @Autowired
    private PostHelper postHelper;
    @Autowired
    private UploadRecordService uploadRecordService;
    @Autowired
    private ProgramInfoService programInfoService;
    @Autowired
    private DeviceInfoService deviceInfoService;

    @PostMapping(value = "record")
    @ApiOperation(value = "上传视频监控信息", notes = "上传视频监控信息", produces = "application/json")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "serialNo", value = "摄像头物理编号", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "camNo", value = "摄像头编号", required = true, paramType = "query", dataType = "int"),
                    @ApiImplicitParam(name = "coverUrl", value = "视频的封面图片", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "playUrl", value = "可直接播放的视频流地址，如果该项有值平台", required = true, paramType = "query", dataType = "String"),
            })
    public JSONResult<String> postVideo(@PathVariable("version") String version, String serialNo,Integer camNo,String coverUrl,String playUrl) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            List<String> keyAndSecret = programInfoService.getKeyAndSecret(serialNo);
            if (keyAndSecret == null) {
                return JSONResult.errorMsg("设备未注册，获取项目信息失败");
            }

            List<VideoRecord> list = new ArrayList<VideoRecord>();
            VideoRecord record = new VideoRecord();
            record.setSerialNo(serialNo);
            record.setCamNo(camNo);
            record.setCoverUrl(coverUrl);
            record.setPlayUrl(playUrl);

            int recordId = uploadRecordService.insertAndGetId(serialNo, new Date(), record.toString(), "视频监控信息");
            if (recordId == 0) {
                String errorInfo = "保存视频监控信息上传记录出错：" + record.toString();
                logger.error(errorInfo);
                return JSONResult.errorMsg(errorInfo);
            } else {
                record.setSourceId(String.valueOf(recordId));
            }
            list.add(record);
            return postHelper.postVideoRecord(keyAndSecret.get(0), keyAndSecret.get(1), list);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return JSONResult.errorMsg("上传视频监控信息失败:" + e.getMessage());
        }
    }

}
